package com.program.utils;

import com.program.services.AirplaneService;
import java.io.*;

/**
 * @program: AirportSystem
 * @description: 数据处理工具类
 * @author: 19071027
 * @create: 2021-09-07 19:19
 **/
public class DataProcessing {

    static AirplaneService airplaneService = AirplaneService.getAirplaneService();

    /**
     * 从Txt文件中读取字符
     *
     * @return 时刻表的字符串数组
     */
    public static String[] getDataInTxt() {
        //存放航班时刻表数据的数组
        String[] data = new String[100];

        //将存放航班时刻表数据的数组转移到这个数组
        String[] mData = null;

        //航班数目
        int num = 0;

        File directory = new File("");
        String filePath = null;
        try {
           filePath = directory.getCanonicalPath()+"\\src\\main\\resources\\航班时刻表2.txt";
            System.out.println(filePath);
        } catch (IOException e){
            e.printStackTrace();
        }

        File dataFile = new File(filePath);
        if (!dataFile.exists()) {
            System.out.println("未找到数据文件");
        }

        try {
            BufferedReader reader = new BufferedReader(new FileReader(dataFile));
            String str;

            while ((str = reader.readLine()) != null) {
                data[num++] = str;
            }
            reader.close();

            airplaneService.setNum(num);

            mData = new String[num];
            for (int i = 0; i < num; i++) {
                mData[i] = data[i];
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return mData;
    }

    /**
     * @param dataStr 航班信息字符串
     * @return 时间整数数组 [0]:起飞小时 [1]:起飞分钟  [2]:降落小时  [3]:降落分钟
     */
    public static String[] getTimeData(String dataStr) {
        //将航班信息分割，获取时间字符串
        String[] str = dataStr.split(",");

        //时间字符串数组 [0]:起飞时间 [1]:降落时间
        String[] timeStr = {str[1], str[2]};

        //时间为0则无效
        String INVALID_TIME = "0";

        //起飞时间字符串 如：7:45
        String flyTimeStr = timeStr[0];

        //降落时间字符串
        String landTimeStr = timeStr[1];

        //整数形式的时间数组  [0]:起飞小时 [1]:起飞分钟  [2]:降落小时  [3]:降落分钟
        String[] time = new String[4];

        if (INVALID_TIME.equals(flyTimeStr)) {
            //如果起飞时间为0，则该航班只在机场降落
            time[0] = "0";
            time[1] = "0";
            time[2] = timeStr[1].split(":")[0];
            time[3] = timeStr[1].split(":")[1];
        }
        else {
            //不为0，该航班在机场降落
            time[0] = timeStr[0].split(":")[0];
            time[1] = timeStr[0].split(":")[1];
            time[2] = "0";
            time[3] = "0";
        }

        return time;

    }

}
